package org.c41.graphics.effect;

import org.c41.graphics.Bounds;
import org.c41.graphics.C41Graphic;
import org.c41.graphics.GraphicsUtil;
import org.c41.util.Check;

public class Close extends BoundsEffect {

	public static final int Horizontal = 0;
	public static final int Vertical = 1;
	
	private long end_time;
	private int direction = 0;
	
	public Close(Paintable base, long time) {
		this(base, time, Horizontal);
	}

	public Close(Paintable base, long time, int direction){
		super(GraphicsUtil.createBufferedImage(base));
		Check.greater(time, 0L);
		Check.between(direction, 0, 1);
		
		this.end_time=time;
		this.direction=direction;
	}
	
	@Override
	protected void updateBounds(long current, Bounds bounds) {
		if(current>this.end_time){
			complete();
			return;
		}
		if(direction==Horizontal){
			updateH(current,bounds);
		}
		else{
			updateV(current,bounds);
		}
	}

	private void updateV(long current, Bounds bounds) {
		int offset = (int)(
				((double)current)
				/(double)this.end_time
				*(double)getHeight()
				/2.0);
		bounds.y= offset;
		bounds.height = getHeight() - offset*2;
	}

	private void updateH(long current, Bounds bounds) {
		int offset = (int)(
				((double)current)
				/(double)this.end_time
				*(double)getWidth()
				/2.0);
		bounds.x= offset;
		bounds.width = getWidth()-offset*2;
	}

	@Override
	public void paintCompleted(C41Graphic g) {
		// no painting
	}
	
}
